<template>
  <div class="register" :style="bacpic">
    <div class="box">
      <div class="title">用户注册</div>
      <el-form
        class="register-form"
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
      >
        <el-form-item prop="phone">
          <el-input
            placeholder="请输入账号"
            v-model="ruleForm.phone"
            size="large"
            type="text"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            placeholder="请输入密码"
            size="large"
            v-model="ruleForm.pass"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item prop="name">
          <el-input
            placeholder="请输入姓名"
            v-model="ruleForm.name"
            type="text"
            size="large"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item prop="role">
          <el-select
            v-model="ruleForm.role"
            placeholder="请选择角色"
            size="large"
          >
            <el-option
              v-for="item in rolelist"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <div class="text">
          <span @click="goRegister">去登陆</span>
        </div>
        <el-form-item>
          <el-button class="btn" type="primary" @click="submitForm(ruleFormRef)"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { role_list, user_register } from "../api/index";
import bac from "../assets/bac.png";
import { useRouter } from "vue-router";

import { ElNotification } from 'element-plus'
import "element-plus/es/components/notification/style/css"

var $router = useRouter();

const ruleForm = reactive({ phone: "", pass: "", name: "", role: "" });
var ruleFormRef = ref();

var rolelist = ref([]);

onMounted(async () => {
  var res = await role_list();
  var arr = res.data.list.map((item) => {
    return { label: item.name, value: item._id };
  });
  rolelist.value = arr;
});

const validatePhone = (rule, value, callback) => {
  if ( /^1[3-9]\d{9}$/.test( value ) == false ) {
    callback(new Error('请输入正确的手机号'))
  } else {
    callback()
  }
}
const validatePass = (rule, value, callback) => {
  if ( /^\d{4}$/.test( value ) == false ) {
    callback(new Error('请输入符合要求的密码,必须是4位数字'))
  } else {
    callback()
  }
}
const validateName = (rule, value, callback) => {
  if ( value == '' ) {
    callback(new Error('请输入姓名'))
  } else {
    callback()
  }
}
const validateRole = (rule, value, callback) => {
  if ( value == '' ) {
    callback(new Error('请选择一个角色'))
  } else {
    callback()
  }
}

//表单验证规则
const rules = reactive({
  phone: [{ validator: validatePhone, trigger: 'blur' }],
  pass: [{ validator: validatePass, trigger: 'blur' }],
  name: [{ validator: validateName, trigger: 'blur' }],
  role: [{ validator: validateRole, trigger: 'blur' }],
})

const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => { 
    if (valid) {
      user_register(ruleForm).then((res)=>{

        if( res.data.code == 200 ){
          ElNotification({
            message: res.data.msg,
            type: 'success',
          })
          $router.push('/login');
        }else{
          ElNotification({
            message: res.data.msg,
            type: 'error',
          })
        }
      })
    } else {
      return false
    }
  })
}

var bacpic = ref({
  backgroundImage: "url(" + bac + ")",
  backgroundRepeat: "no-repeat",
  backgroundSize: "cover",
  minHeight: "100vh",
  position: "relative",
  backgroundPosition: "center center",
});

var goRegister = () => {
  $router.push("/login");
};
</script>

<style scoped lang="scss">
.register {
  overflow: hidden;
  .box {
    width: 300px;
    margin: 150px auto;
    background-color: white;
    padding: 20px 40px 20px;
    .title {
      line-height: 30px;
    }
    .text {
      line-height: 25px;
      font-size: 12px;
      color: lightskyblue;
      cursor: pointer;
    }
    .btn {
      margin: 30px auto;
      padding: 0 98px;
    }
  }
}
</style>